﻿using HalconDotNet;
using MultiAxisAssembly.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZRFCore.Camera;
using ZRFCore.MotionControlCard;
using ZRFCore.Tool;

namespace ZRFCore.ToolForm
{
    public partial class Calib_ByNinePoint : Form
    {
        ICamera camera;
        string camParamPath;
        string camPosePath;
        Machine machine;

        string calibPath = Directory.GetCurrentDirectory() + "/Calib/";

        double[] imgR = new double[9];
        double[] imgC = new double[9];
        double[] machInX = new double[9];
        double[] machInY = new double[9];
        double[] machOutX = new double[9];
        double[] machOutY = new double[9];
        MulitAxisMasterV2 mulitAxisMasterV2 = new MulitAxisMasterV2();
        public Calib_ByNinePoint()
        {
            InitializeComponent();
            camParamPath = "";
            camPosePath = "";
            machine = new Machine();
            machine.Init();
            mulitAxisMasterV2.XAxis = machine.XAxis;
            mulitAxisMasterV2.YAxis = machine.YAxis;
            mulitAxisMasterV2.ZAxis = machine.ZAxis;
            this.KeyDown += mulitAxisMasterV2.KeyDown;
            this.KeyUp += mulitAxisMasterV2.KeyUp;
            camera = new Camera_HKVision();
            Camera_HKVision.EnumDevicesList();
            camera.CreateDevice();
            camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
            camera.Open();
        }

        private void Calib_ByNinePoint_FormClosing(object sender, FormClosingEventArgs e)
        {
            camera.Close();
            machine.Close();
        }

        private void Camera_ImageCallbackEvent(ICamera sender, ImageCallbackEventArgs e)
        {
            zrf_hWindow1.DispImage(e.HImage);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            try
            {
                camera.StartGrabbing();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            try
            {
                camera.StopGrabbing();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            zrf_hWindow1.HRegions.Clear();
            zrf_hWindow1.DrawCircle();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //模板匹配mark点
            // HObject region = zrf_hWindow1.HRegions[0].HObject;
            try
            {
                action1();
                String str = "";
                for (int i = 0; i < imgR.Length; i++)
                {
                    str += i + 1 + "[R:" + imgR[i] + "   C:" + imgC[i] + "]\r\n";

                }
                textBox1.Text = str;
            }
            catch (Exception ee)
            {

                MessageBox.Show(ee.Message);
            }

        }

        //private void action1()
        //{


        //    // Stack for temporary objects 
        //    HObject[] OTemp = new HObject[20];

        //    // Local iconic variables 

        //    HObject ho_Image, ho_circle, ho_ModelContours;
        //    HObject ho_MarkRegions, ho_ContoursAffineTrans = null, ho_Region = null;
        //    HObject ho_SortedRegions, ho_ObjectSelected = null, ho_RegionErosion = null;
        //    HObject ho_ImageReduced = null, ho_Region1 = null, ho_ConnectedRegions1 = null;
        //    HObject ho_SelectedRegions1 = null;

        //    // Local control variables 

        //    HTuple hv_CamParamer = new HTuple(), hv_CamPos = new HTuple();
        //    HTuple hv_picPointCenterR = new HTuple(), hv_picPointCenterC = new HTuple();
        //    HTuple hv_WindowHandle = new HTuple(), hv_machX = new HTuple();
        //    HTuple hv_machY = new HTuple(), hv_ModelID = new HTuple();
        //    HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
        //    HTuple hv_Angle = new HTuple(), hv_Scale = new HTuple();
        //    HTuple hv_Score = new HTuple(), hv_Index1 = new HTuple();
        //    HTuple hv_HomMat2D1 = new HTuple(), hv_HomMat2DScale = new HTuple();
        //    HTuple hv_MarkRegionsNum = new HTuple(), hv_Number = new HTuple();
        //    HTuple hv_Index = new HTuple(), hv_UsedThreshold = new HTuple();
        //    HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
        //    HTuple hv_Radius = new HTuple(), hv_wX = new HTuple();
        //    HTuple hv_wY = new HTuple(), hv_HomMat2D = new HTuple();
        //    HTuple hv_Tuple = new HTuple(), hv_Qx = new HTuple(), hv_Qy = new HTuple();
        //    // Initialize local and output iconic variables 
        //    HOperatorSet.GenEmptyObj(out ho_Image);
        //    HOperatorSet.GenEmptyObj(out ho_circle);
        //    HOperatorSet.GenEmptyObj(out ho_ModelContours);
        //    HOperatorSet.GenEmptyObj(out ho_MarkRegions);
        //    HOperatorSet.GenEmptyObj(out ho_ContoursAffineTrans);
        //    HOperatorSet.GenEmptyObj(out ho_Region);
        //    HOperatorSet.GenEmptyObj(out ho_SortedRegions);
        //    HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
        //    HOperatorSet.GenEmptyObj(out ho_RegionErosion);
        //    HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        //    HOperatorSet.GenEmptyObj(out ho_Region1);
        //    HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
        //    HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
        //    hv_CamParamer.Dispose();
        //    HOperatorSet.ReadCamPar(camParamPath, out hv_CamParamer);
        //    hv_CamPos.Dispose();
        //    HOperatorSet.ReadPose(camPosePath, out hv_CamPos);
        //    ho_Image.Dispose();
        //    //HOperatorSet.ReadImage(out ho_Image, "F:/工作-视觉/20220629 视觉喷胶机V2.0/视觉喷点一体机/视觉/摄像机标定/Image_20220810134806299.bmp");
        //    if (zrf_hWindow1.Image == null)
        //    {
        //        return;
        //    }
        //    ho_Image = zrf_hWindow1.Image;
        //    hv_picPointCenterR.Dispose();
        //    hv_picPointCenterR = new HTuple();
        //    hv_picPointCenterC.Dispose();
        //    hv_picPointCenterC = new HTuple();

        //    if (HDevWindowStack.IsOpen())
        //    {
        //        hv_WindowHandle = HDevWindowStack.GetActive();
        //    }
        //    hv_machX.Dispose();
        //    hv_machX = new HTuple();
        //    hv_machX = machInX;
        //    hv_machY.Dispose();
        //    hv_machY = new HTuple();
        //    hv_machY = machInY;

        //    ho_circle.Dispose();
        //    HOperatorSet.GenCircleContourXld(out ho_circle, 0, 0, 112.795, 0, 6.28318, "positive",
        //        1);

        //    hv_ModelID.Dispose();
        //    HOperatorSet.CreateScaledShapeModelXld(ho_circle, "auto", 0, 0, "auto", 0.8,
        //        1.2, "auto", "auto", "ignore_local_polarity", 8, out hv_ModelID);
        //    ho_ModelContours.Dispose();
        //    HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);

        //    HOperatorSet.DispObj(ho_Image, zrf_hWindow1.HalconWindow);

        //    hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Angle.Dispose(); hv_Scale.Dispose(); hv_Score.Dispose();
        //    HOperatorSet.FindScaledShapeModel(ho_Image, hv_ModelID, 0, 0, 0.8, 1.2, 0.5,
        //        0, 0.5, "least_squares", 0, 0.9, out hv_Row1, out hv_Column1, out hv_Angle,
        //        out hv_Scale, out hv_Score);
        //    ho_MarkRegions.Dispose();
        //    HOperatorSet.GenEmptyRegion(out ho_MarkRegions);
        //    if ((int)(new HTuple((new HTuple(hv_Row1.TupleLength())).TupleGreater(0))) != 0)
        //    {
        //        for (hv_Index1 = 0; (int)hv_Index1 <= (int)((new HTuple(hv_Row1.TupleLength())) - 1); hv_Index1 = (int)hv_Index1 + 1)
        //        {

        //            hv_HomMat2D1.Dispose();
        //            HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_Row1.TupleSelect(hv_Index1),
        //                hv_Column1.TupleSelect(hv_Index1), hv_Angle.TupleSelect(hv_Index1), out hv_HomMat2D1);


        //            hv_HomMat2DScale.Dispose();
        //            HOperatorSet.HomMat2dScale(hv_HomMat2D1, hv_Scale.TupleSelect(hv_Index1),
        //                hv_Scale.TupleSelect(hv_Index1), hv_Row1.TupleSelect(hv_Index1), hv_Column1.TupleSelect(
        //                hv_Index1), out hv_HomMat2DScale);

        //            ho_ContoursAffineTrans.Dispose();
        //            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffineTrans,
        //                hv_HomMat2DScale);

        //            HOperatorSet.DispObj(ho_ContoursAffineTrans, zrf_hWindow1.HalconWindow);

        //            ho_Region.Dispose();
        //            HOperatorSet.GenRegionContourXld(ho_ContoursAffineTrans, out ho_Region, "filled");
        //            {
        //                HObject ExpTmpOutVar_0;
        //                HOperatorSet.ConcatObj(ho_MarkRegions, ho_Region, out ExpTmpOutVar_0);
        //                ho_MarkRegions.Dispose();
        //                ho_MarkRegions = ExpTmpOutVar_0;
        //            }
        //        }
        //    }
        //    hv_MarkRegionsNum.Dispose();
        //    HOperatorSet.CountObj(ho_MarkRegions, out hv_MarkRegionsNum);
        //    if ((int)(new HTuple((new HTuple(hv_MarkRegionsNum.TupleLength())).TupleNotEqual(
        //        9))) != 0)
        //    {

        //        HOperatorSet.DispText(zrf_hWindow1.HalconWindow, "找到得数量不足9",
        //            "window", 20, 20, "red", new HTuple(), new HTuple());
        //        return;
        //    }
        //    else
        //    {

        //        HOperatorSet.DispText(zrf_hWindow1.HalconWindow, "找到9个", "window",
        //            20, 20, "green", new HTuple(), new HTuple());

        //    }

        //    // stop(...); only in hdevelop

        //    ho_SortedRegions.Dispose();
        //    HOperatorSet.SortRegion(ho_MarkRegions, out ho_SortedRegions, "character", "true",
        //        "row");
        //    hv_Number.Dispose();
        //    HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);

        //    HTuple end_val39 = hv_Number;
        //    HTuple step_val39 = 1;
        //    for (hv_Index = 1; hv_Index.Continue(end_val39, step_val39); hv_Index = hv_Index.TupleAdd(step_val39))
        //    {
        //        ho_ObjectSelected.Dispose();
        //        HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_Index);
        //        ho_RegionErosion.Dispose();
        //        HOperatorSet.ErosionCircle(ho_ObjectSelected, out ho_RegionErosion, 20);

        //        ho_ImageReduced.Dispose();
        //        HOperatorSet.ReduceDomain(ho_Image, ho_RegionErosion, out ho_ImageReduced);

        //        //threshold (ImageReduced, Regions1, 0, 82)
        //        ho_Region1.Dispose(); hv_UsedThreshold.Dispose();
        //        HOperatorSet.BinaryThreshold(ho_ImageReduced, out ho_Region1, "max_separability",
        //            "dark", out hv_UsedThreshold);
        //        //threshold (ImageReduced, Regions1, 0, 30)
        //        ho_ConnectedRegions1.Dispose();
        //        HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions1);
        //        ho_SelectedRegions1.Dispose();
        //        HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area",
        //            "and", 30, 2000);
        //        hv_Row.Dispose(); hv_Column.Dispose(); hv_Radius.Dispose();
        //        HOperatorSet.SmallestCircle(ho_SelectedRegions1, out hv_Row, out hv_Column,
        //            out hv_Radius);
        //        //area_center (SelectedRegions1, Area, Row, Column)
        //        if (hv_picPointCenterR == null)
        //            hv_picPointCenterR = new HTuple();
        //        hv_picPointCenterR[hv_Index - 1] = hv_Row;
        //        if (hv_picPointCenterC == null)
        //            hv_picPointCenterC = new HTuple();
        //        hv_picPointCenterC[hv_Index - 1] = hv_Column;
        //        // stop(...); only in hdevelop
        //    }

        //    //img to machcoord
        //    hv_wX.Dispose(); hv_wY.Dispose();
        //    HOperatorSet.ImagePointsToWorldPlane(hv_CamParamer, hv_CamPos, hv_picPointCenterR,
        //        hv_picPointCenterC, "mm", out hv_wX, out hv_wY);
        //    hv_HomMat2D.Dispose();
        //    HOperatorSet.VectorToHomMat2d(hv_wX, hv_wY, hv_machX, hv_machY, out hv_HomMat2D);
        //    HOperatorSet.WriteTuple(hv_HomMat2D, Directory.GetCurrentDirectory() + "Calib/HomMat2D.tup");
        //    //QX为第一个点的机械坐标X。
        //    //QY为第一个点的机械坐标Y。
        //    hv_Qx.Dispose(); hv_Qy.Dispose();
        //    HOperatorSet.AffineTransPoint2d(hv_HomMat2D, hv_wX, hv_wY, out hv_Qx, out hv_Qy);
        //    machOutX = hv_Qx;
        //    machOutY = hv_Qy;

        //    ho_Image.Dispose();
        //    ho_circle.Dispose();
        //    ho_ModelContours.Dispose();
        //    ho_MarkRegions.Dispose();
        //    ho_ContoursAffineTrans.Dispose();
        //    ho_Region.Dispose();
        //    ho_SortedRegions.Dispose();
        //    ho_ObjectSelected.Dispose();
        //    ho_RegionErosion.Dispose();
        //    ho_ImageReduced.Dispose();
        //    ho_Region1.Dispose();
        //    ho_ConnectedRegions1.Dispose();
        //    ho_SelectedRegions1.Dispose();

        //    hv_CamParamer.Dispose();
        //    hv_CamPos.Dispose();
        //    hv_picPointCenterR.Dispose();
        //    hv_picPointCenterC.Dispose();
        //    hv_WindowHandle.Dispose();
        //    hv_machX.Dispose();
        //    hv_machY.Dispose();
        //    hv_ModelID.Dispose();
        //    hv_Row1.Dispose();
        //    hv_Column1.Dispose();
        //    hv_Angle.Dispose();
        //    hv_Scale.Dispose();
        //    hv_Score.Dispose();
        //    hv_Index1.Dispose();
        //    hv_HomMat2D1.Dispose();
        //    hv_HomMat2DScale.Dispose();
        //    hv_MarkRegionsNum.Dispose();
        //    hv_Number.Dispose();
        //    hv_Index.Dispose();
        //    hv_UsedThreshold.Dispose();
        //    hv_Row.Dispose();
        //    hv_Column.Dispose();
        //    hv_Radius.Dispose();
        //    hv_wX.Dispose();
        //    hv_wY.Dispose();
        //    hv_HomMat2D.Dispose();
        //    hv_Tuple.Dispose();
        //    hv_Qx.Dispose();
        //    hv_Qy.Dispose();

        //}

        private void action1()//图像像素位置
        {


            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];

            // Local iconic variables 

            HObject ho_Image, ho_circle, ho_ModelContours;
            HObject ho_MarkRegions, ho_ContoursAffineTrans = null, ho_Region = null;
            HObject ho_SortedRegions, ho_ObjectSelected = null, ho_RegionErosion = null;
            HObject ho_ImageReduced = null, ho_Region1 = null, ho_ConnectedRegions1 = null;
            HObject ho_SelectedRegions1 = null;

            // Local control variables 

            HTuple hv_ModelID = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple(), hv_Angle = new HTuple();
            HTuple hv_Scale = new HTuple(), hv_Score = new HTuple();
            HTuple hv_Index1 = new HTuple(), hv_HomMat2D1 = new HTuple();
            HTuple hv_HomMat2DScale = new HTuple(), hv_MarkRegionsNum = new HTuple();
            HTuple hv_Number = new HTuple(), hv_picPointCenterR = new HTuple();
            HTuple hv_picPointCenterC = new HTuple(), hv_Index = new HTuple();
            HTuple hv_UsedThreshold = new HTuple(), hv_Row = new HTuple();
            HTuple hv_Column = new HTuple(), hv_Radius = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_circle);
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            HOperatorSet.GenEmptyObj(out ho_MarkRegions);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffineTrans);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            ho_Image.Dispose();
            //HOperatorSet.ReadImage(out ho_Image, "F:/工作-视觉/20220629 视觉喷胶机V2.0/视觉喷点一体机/视觉/摄像机标定/Image_20220810134806299.bmp");
            if (zrf_hWindow1.Image == null)
            {
                return;
            }
            ho_Image = zrf_hWindow1.Image.Clone();
            HOperatorSet.SetColor(zrf_hWindow1.HalconWindow, "red");
            ho_circle.Dispose();
            HOperatorSet.GenCircleContourXld(out ho_circle, 0, 0, 112.795, 0, 6.28318, "positive",
                1);
            hv_ModelID.Dispose();
            HOperatorSet.CreateScaledShapeModelXld(ho_circle, "auto", 0, 0, "auto", 0.7,
                1.5, "auto", "auto", "ignore_local_polarity", 8, out hv_ModelID);
            ho_ModelContours.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);

            HOperatorSet.DispObj(ho_Image, zrf_hWindow1.HalconWindow);

            hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Angle.Dispose(); hv_Scale.Dispose(); hv_Score.Dispose();
            HOperatorSet.FindScaledShapeModel(ho_Image, hv_ModelID, 0, 0, 0.7, 1.5, 0.75,
                0, 0.5, "least_squares", 0, 0.9, out hv_Row1, out hv_Column1, out hv_Angle,
                out hv_Scale, out hv_Score);
            ho_MarkRegions.Dispose();
            HOperatorSet.GenEmptyObj(out ho_MarkRegions);
            if ((int)(new HTuple((new HTuple(hv_Row1.TupleLength())).TupleGreater(0))) != 0)
            {
                for (hv_Index1 = 0; (int)hv_Index1 <= (int)((new HTuple(hv_Row1.TupleLength())) - 1); hv_Index1 = (int)hv_Index1 + 1)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_HomMat2D1.Dispose();
                        HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_Row1.TupleSelect(hv_Index1),
                            hv_Column1.TupleSelect(hv_Index1), hv_Angle.TupleSelect(hv_Index1), out hv_HomMat2D1);
                    }
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_HomMat2DScale.Dispose();
                        HOperatorSet.HomMat2dScale(hv_HomMat2D1, hv_Scale.TupleSelect(hv_Index1),
                            hv_Scale.TupleSelect(hv_Index1), hv_Row1.TupleSelect(hv_Index1), hv_Column1.TupleSelect(
                            hv_Index1), out hv_HomMat2DScale);
                    }
                    ho_ContoursAffineTrans.Dispose();
                    HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffineTrans,
                        hv_HomMat2DScale);

                    HOperatorSet.DispObj(ho_ContoursAffineTrans, zrf_hWindow1.HalconWindow);

                    ho_Region.Dispose();
                    HOperatorSet.GenRegionContourXld(ho_ContoursAffineTrans, out ho_Region, "filled");
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.ConcatObj(ho_MarkRegions, ho_Region, out ExpTmpOutVar_0);
                        ho_MarkRegions.Dispose();
                        ho_MarkRegions = ExpTmpOutVar_0;
                    }
                }
            }
            hv_MarkRegionsNum.Dispose();
            HOperatorSet.CountObj(ho_MarkRegions, out hv_MarkRegionsNum);
            if ((int)(new HTuple(hv_MarkRegionsNum.TupleNotEqual(9))) != 0)
            {

                HOperatorSet.DispText(zrf_hWindow1.HalconWindow, "找到得数量不足9",
                    "window", 20, 20, "red", new HTuple(), new HTuple());

            }
            else
            {

                HOperatorSet.DispText(zrf_hWindow1.HalconWindow, "找到9个", "window",
                    20, 20, "green", new HTuple(), new HTuple());

            }
            // stop(...); only in hdevelop

            ho_SortedRegions.Dispose();
            HOperatorSet.SortRegion(ho_MarkRegions, out ho_SortedRegions, "character", "true",
                "row");
            hv_Number.Dispose();
            HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);
            hv_picPointCenterR.Dispose();
            hv_picPointCenterR = new HTuple();
            hv_picPointCenterC.Dispose();
            hv_picPointCenterC = new HTuple();
            HTuple end_val30 = hv_Number;
            HTuple step_val30 = 1;
            for (hv_Index = 1; hv_Index.Continue(end_val30, step_val30); hv_Index = hv_Index.TupleAdd(step_val30))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_Index);
                ho_RegionErosion.Dispose();
                HOperatorSet.ErosionCircle(ho_ObjectSelected, out ho_RegionErosion, 20);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_RegionErosion, out ho_ImageReduced);
                ho_Region1.Dispose(); hv_UsedThreshold.Dispose();
                HOperatorSet.BinaryThreshold(ho_ImageReduced, out ho_Region1, "max_separability",
                    "dark", out hv_UsedThreshold);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions1);
                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area",
                    "and", 30, 2000);
                hv_Row.Dispose(); hv_Column.Dispose(); hv_Radius.Dispose();
                HOperatorSet.SmallestCircle(ho_SelectedRegions1, out hv_Row, out hv_Column,
                    out hv_Radius);
                if (hv_picPointCenterR == null)
                    hv_picPointCenterR = new HTuple();
                hv_picPointCenterR[hv_Index - 1] = hv_Row;
                if (hv_picPointCenterC == null)
                    hv_picPointCenterC = new HTuple();
                hv_picPointCenterC[hv_Index - 1] = hv_Column;
                // stop(...); only in hdevelop
            }
            imgR = hv_picPointCenterR.Clone();
            imgC = hv_picPointCenterC.Clone();


            ho_Image.Dispose();
            ho_circle.Dispose();
            ho_ModelContours.Dispose();
            ho_MarkRegions.Dispose();
            ho_ContoursAffineTrans.Dispose();
            ho_Region.Dispose();
            ho_SortedRegions.Dispose();
            ho_ObjectSelected.Dispose();
            ho_RegionErosion.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region1.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_SelectedRegions1.Dispose();

            hv_ModelID.Dispose();
            hv_Row1.Dispose();
            hv_Column1.Dispose();
            hv_Angle.Dispose();
            hv_Scale.Dispose();
            hv_Score.Dispose();
            hv_Index1.Dispose();
            hv_HomMat2D1.Dispose();
            hv_HomMat2DScale.Dispose();
            hv_MarkRegionsNum.Dispose();
            hv_Number.Dispose();
            hv_picPointCenterR.Dispose();
            hv_picPointCenterC.Dispose();
            hv_Index.Dispose();
            hv_UsedThreshold.Dispose();
            hv_Row.Dispose();
            hv_Column.Dispose();
            hv_Radius.Dispose();

        }


        private void button16_Click(object sender, EventArgs e)
        {
            camParamPath = ZRFCore.Tool.FileHelper.OpenFile();
        }

        private void button17_Click(object sender, EventArgs e)
        {
            camPosePath = ZRFCore.Tool.FileHelper.OpenFile();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            try
            {
                action2();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

        }

        private void action2()
        {


            // Local iconic variables 

            // Local control variables 

            HTuple hv_CamParamer = new HTuple(), hv_CamPos = new HTuple();
            HTuple hv_machX = new HTuple(), hv_machY = new HTuple();
            HTuple hv_picPointCenterR = new HTuple(), hv_picPointCenterC = new HTuple();
            HTuple hv_wX = new HTuple(), hv_wY = new HTuple(), hv_HomMat2D = new HTuple();
            HTuple hv_Qx = new HTuple(), hv_Qy = new HTuple();
            // Initialize local and output iconic variables 
            hv_CamParamer.Dispose();
            HOperatorSet.ReadCamPar(camParamPath, out hv_CamParamer);
            hv_CamPos.Dispose();
            HOperatorSet.ReadPose(camPosePath, out hv_CamPos);

            hv_machX.Dispose();
            hv_machX = new HTuple();
            hv_machY.Dispose();
            hv_machY = new HTuple();
            hv_picPointCenterR.Dispose();
            hv_picPointCenterR = new HTuple();
            hv_picPointCenterC.Dispose();
            hv_picPointCenterC = new HTuple();

            //img to machcoord
            hv_wX.Dispose(); hv_wY.Dispose();
            hv_picPointCenterR = imgR;
            hv_picPointCenterC = imgC;
            HOperatorSet.ImagePointsToWorldPlane(hv_CamParamer, hv_CamPos, hv_picPointCenterR, hv_picPointCenterC, "mm", out hv_wX, out hv_wY);
            hv_HomMat2D.Dispose();
            hv_machX = machInX;
            hv_machY = machInY;
            HOperatorSet.VectorToHomMat2d(hv_wX, hv_wY, hv_machX, hv_machY, out hv_HomMat2D);
            HOperatorSet.WriteTuple(hv_HomMat2D, calibPath + "HomMat2D");
            HOperatorSet.WriteCamPar(hv_CamParamer, calibPath + "CamParamer");
            HOperatorSet.WritePose(hv_CamPos, calibPath + "CamPos");

            //QX为第一个点的机械坐标X。
            //QY为第一个点的机械坐标Y。
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_Qx.Dispose(); hv_Qy.Dispose();
                HOperatorSet.AffineTransPoint2d(hv_HomMat2D, hv_wX, hv_wY, out hv_Qx, out hv_Qy);
                machOutX = hv_Qx.Clone();
                machOutY = hv_Qy.Clone();
            }

            hv_CamParamer.Dispose();
            hv_CamPos.Dispose();
            hv_machX.Dispose();
            hv_machY.Dispose();
            hv_picPointCenterR.Dispose();
            hv_picPointCenterC.Dispose();
            hv_wX.Dispose();
            hv_wY.Dispose();
            hv_HomMat2D.Dispose();
            hv_Qx.Dispose();
            hv_Qy.Dispose();

        }

        private void buttonMachCoordIn_Click(object sender, EventArgs e)
        {
            string btnName = ((Button)sender).Name;
            int index = int.Parse(btnName.Remove(0, 6));
            if (mulitAxisMasterV2.XAxis != null) machInX[index - 3] = mulitAxisMasterV2.XAxis.DPos;
            if (mulitAxisMasterV2.YAxis != null) machInY[index - 3] = mulitAxisMasterV2.YAxis.DPos;

            string str = string.Format("X:{0:N2}\r\nY:{1:N2}", machInX[index - 3], machInY[index - 3]);
            ((Button)sender).Text = str;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (mulitAxisMasterV2.XAxis != null) label5.Text = mulitAxisMasterV2.XAxis.DPos.ToString();
                if (mulitAxisMasterV2.YAxis != null) label6.Text = mulitAxisMasterV2.YAxis.DPos.ToString();
                if (mulitAxisMasterV2.ZAxis != null) label7.Text = mulitAxisMasterV2.ZAxis.DPos.ToString();
                if (mulitAxisMasterV2.RAxis != null) label8.Text = mulitAxisMasterV2.RAxis.DPos.ToString();
                if (mulitAxisMasterV2.XAxis != null) label9.Text = mulitAxisMasterV2.XAxis.Speed.ToString();
            }
            catch
            {

            }

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string name = button1.Text;
            try
            {
                button1.Text = "保存中...";
                AxisesLocation axisesLocation = new AxisesLocation();
                if (mulitAxisMasterV2.XAxis != null) axisesLocation.XPos = mulitAxisMasterV2.XAxis.DPos;
                if (mulitAxisMasterV2.YAxis != null) axisesLocation.YPos = mulitAxisMasterV2.YAxis.DPos;
                if (mulitAxisMasterV2.ZAxis != null) axisesLocation.ZPos = mulitAxisMasterV2.ZAxis.DPos;
                if (mulitAxisMasterV2.RAxis != null) axisesLocation.RPos = mulitAxisMasterV2.RAxis.DPos;

                XmlHelper.XmlSerializeToFile(axisesLocation, calibPath + "SnapPos.xml", Encoding.UTF8);

                Task.Run(() => { Thread.Sleep(1000); this.Invoke(() => { button1.Text = name; }); });
            }
            catch (Exception ee)
            {

                MessageBox.Show(ee.Message);
                button1.Text = name;
            }

        }

        private bool isGotoSnapPos = false;
        private void button12_Click(object sender, EventArgs e)
        {
            string name = button12.Text;
            try
            {
                button12.Text = "移动中...";
                AxisesLocation axisesLocation = XmlHelper.XmlDeserializeFromFile<AxisesLocation>(calibPath + "SnapPos.xml", Encoding.UTF8);

                double XPos = 0;
                double YPos = 0;
                double ZPos = 0;
                double RPos = 0;

                if (mulitAxisMasterV2.XAxis != null) XPos = axisesLocation.XPos;
                if (mulitAxisMasterV2.YAxis != null) YPos = axisesLocation.YPos;
                if (mulitAxisMasterV2.ZAxis != null) ZPos = axisesLocation.ZPos;
                if (mulitAxisMasterV2.RAxis != null) RPos = axisesLocation.RPos;

                if (isGotoSnapPos == false)
                {
                    isGotoSnapPos = true;
                    Task.Run(() =>
                    {
                        if (mulitAxisMasterV2.XAxis != null) mulitAxisMasterV2.XAxis.MoveAbs((float)XPos);
                        if (mulitAxisMasterV2.YAxis != null) mulitAxisMasterV2.YAxis.MoveAbs((float)YPos);

                        if (mulitAxisMasterV2.XAxis != null) mulitAxisMasterV2.XAxis.WaitMoveStop();
                        if (mulitAxisMasterV2.YAxis != null) mulitAxisMasterV2.YAxis.WaitMoveStop();



                        if (mulitAxisMasterV2.RAxis != null) mulitAxisMasterV2.RAxis.MoveAbs((float)RPos);
                        if (mulitAxisMasterV2.RAxis != null) mulitAxisMasterV2.RAxis.WaitMoveStop();


                        if (mulitAxisMasterV2.ZAxis != null) mulitAxisMasterV2.ZAxis.MoveAbs((float)ZPos);
                        if (mulitAxisMasterV2.ZAxis != null) mulitAxisMasterV2.ZAxis.WaitMoveStop();

                        isGotoSnapPos = false;
                        this.Invoke(() => { button12.Text = name; });
                    });
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                button12.Text = name;
            }
        }

        private bool testMark = false;
        private void button15_Click(object sender, EventArgs e)
        {
            string str = button15.Name;
            if (testMark == true) return;
            testMark = true;
            button15.Name = "测试中...";
            Task.Run(() =>
            {
                for (int i = 0; i < machOutX.Length; i++)
                {
                    if (mulitAxisMasterV2.XAxis != null) mulitAxisMasterV2.XAxis.MoveAbs((float)machOutX[i]);
                    if (mulitAxisMasterV2.YAxis != null) mulitAxisMasterV2.YAxis.MoveAbs((float)machOutY[i]);

                    if (mulitAxisMasterV2.XAxis != null) mulitAxisMasterV2.XAxis.WaitMoveStop();
                    if (mulitAxisMasterV2.YAxis != null) mulitAxisMasterV2.YAxis.WaitMoveStop();

                    Thread.Sleep(100);
                }
                this.Invoke(() =>
                {
                    button15.Name = str;
                });
                testMark = false;
            });

        }
    }
}
